package com.iweb.oasys.service;

import com.iweb.oasys.dao.UserDao;
import com.iweb.oasys.dao.UserDaoImpl;
import com.iweb.oasys.entity.User;
import com.iweb.oasys.util.ResponseBody;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

public class UserServiceImpl implements UserService{
    private static UserService userService = new UserServiceImpl();

    private UserDao userDao = UserDaoImpl.getInstance();

    private UserServiceImpl(){}

    public static UserService getInstance() {
        return userService;
    }

    @Override
    public ResponseBody login(HttpServletRequest request) throws SQLException {
        User user = new User();
        user.setUsername(request.getParameter("username"));
        userDao.userExits(user);
        ResponseBody responseBody = new ResponseBody();
        if (user.getPassword()==null) {
            responseBody.setResCode(1);
            responseBody.setErrMsg("该用户不存在");
        }else if (!user.getPassword().equals(DigestUtils.md5Hex(request.getParameter("password")))) {
            responseBody.setResCode(1);
            responseBody.setErrMsg("密码错误！");
        }else {
            responseBody.setResCode(0);
        }
        return responseBody;
    }
}
